﻿; 脚本由 Inno Setup 脚本向导 生成！
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档！
#define MyAppName "Summer"
#define ClientExeName "SunRiseDemo.exe"    
#define MyAppRegisterGatewayBat "Gateway\RegSysSrv.bat"
#define MyAppUnregisterGatewayBat "Gateway\UnRegSysSrv.bat"
; 默认安装路径
#define MyAppSetupBaseDir "C:\Program Files (x86)\"

;pg 数据库 安装文件
#define pgExe "postgresql-14.6-1-windows-x64.exe"
; 上述消息放置到脚本的顶部

#ifndef Version
  #define Version = 'V1.0.00.000';
#endif

[Registry]
//innostall 制作的安装包，在安装或卸载过程，会向注册表自动写入 或 删除 相关软件信息，如版本号等
//其它用户自定义相的关注册表信息可在此处另单独编写,举例如下：
Root: HKCU ;SubKey:SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers;ValueType:string;ValueName:{app}\Client\{#ClientExeName};ValueData:"RUNASADMIN" ;Flags:uninsdeletekeyifempty uninsdeletevalue;
[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID，可在菜单中点击 "工具|生成 GUID"。)
;AppId={{48E12288-1628-4440-B937-3D750E36C4E5} ,此处去除AppID，目的是在注册表中，已 AppName（Smart Dispatch Plus）作为显示标识
AppName={#MyAppName}
AppVersion={#Version}
;AppVerName={#MyAppName} {#Version}
DefaultDirName={#MyAppSetupBaseDir}{#MyAppName}
DisableDirPage=yes
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
; 以下行取消注释，以在非管理安装模式下运行（仅为当前用户安装）。
PrivilegesRequired=admin
PrivilegesRequiredOverridesAllowed=commandline
OutputDir=.\dist
OutputBaseFilename=Summer {#Version}
SetupIconFile=.\vlc.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
UninstallDisplayName={#MyAppName}
UsePreviousAppDir=no
; 开启日志
SetupLogging=yes
[Languages]
Name: "cn"; MessagesFile: "compiler:Languages\ChineseSimplified.isl"; LicenseFile:.\Licenses\License-CN.txt
Name: "en"; MessagesFile: "compiler:Default.isl"; LicenseFile:.\Licenses\License-EN.txt

[Code] 
function InitializeSetup(): Boolean;
var
  result1:   Integer;
  result2:   Integer;
begin
  // 脚本停止服务、进程        
  ShellExec('', 'cmd.exe', 'cmd /c net stop "MyGateway"', '', SW_HIDE, ewWaitUntilTerminated, result1);  
  ShellExec('', 'cmd.exe', 'cmd /c taskkill /f /im {#ClientExeName}', '', SW_HIDE,  ewWaitUntilTerminated, result2);
  // True：继续， False：不再安装
  Result := True;
end;

function IsFileExist(filePath: string): Boolean;
  begin
    if (FileExists(filePath)) then
      begin
        Result := True;
      end
    else
      begin
        Result := False;
      end;
  end;

// 通过注册表， 检测 PG数据库 是否已经安装
function IsPGNeedInstall(): Boolean; 
// 如下定义变量
// var displayVersion: String;
begin  
  // 如下给变量赋值
  // displayVersion := 'C:\Program Files\PostgreSQL\14\uninstall-postgresql.exe';
  // 因为 32位 和 64位 的缘故，增加 HKML64 判断
  if RegValueExists(HKLM, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PostgreSQL 14', 'DisplayVersion')
     or RegValueExists(HKLM64, 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PostgreSQL 14', 'DisplayVersion') then 
    begin
      Result := False;
    end
  else 
    begin
      Result := True;
    end;
end;

var
  SelectDirPage: TInputDirWizardPage;

procedure InitializeWizard;
begin
  SelectDirPage := CreateInputDirPage(wpSelectDir, 'Select Custom Install Path', 'Select the folder where you want to install the application.', 'Select a folder:', False, '');
  SelectDirPage.Add('');
  SelectDirPage.Values[0] := ExpandConstant('{#MyAppSetupBaseDir}');
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = SelectDirPage.ID then
  begin
    // Set the custom install path
    WizardForm.DirEdit.Text := SelectDirPage.Values[0]+'\{#MyAppName}';
  end;
  Result := True;
end;


[Types]
Name: "Custom"; Description: "Custom Installation"; Flags: iscustom

[Components] 
Name: Client; Description: Client ; Types: Custom 
;通过 \First, \Second 完成二级菜单
Name: Gateway; Description: Gateway ; Types: Custom 
;通过 exclusive 完成 DMR、XPT 的互斥选择            
Name: Gateway\First; Description: Gateway First; Types: Custom; Flags: exclusive;
Name: Gateway\Second; Description: Gateway Second; Types: Custom; Flags: exclusive;  

[Files]          
; onlyifdoesntexist 如果文件存在，则不复制  
Source: "version.txt"; DestDir: "{app}"; Flags: ignoreversion           
; 如果安装了客户端或网关，则复制 Common 下的所有文件，到安装目录下的 Common 文件夹内
Source: "Client\*"; DestDir: "{app}\Client\"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: Client;
Source: "Common\*"; DestDir: "{app}\Common\"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: Client Gateway; 
; 如果安装了First网关，则复制 First 下的所有文件，到安装目录下的 \Gateway\First 文件夹内
Source: "Gateway\First\*"; DestDir: "{app}\Gateway\First\"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: Gateway\First;
Source: "Gateway\Second\*"; DestDir: "{app}\Gateway\Second\"; Flags: ignoreversion recursesubdirs createallsubdirs; Components: Gateway\Second;
; 注意: 不要在任何共享系统文件上使用“Flags: ignoreversion”

[Dirs]
Name:"C:\ProgramData\Record"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: checkablealone

[Icons]
; 开始菜单中创建快捷方式：应用的快捷方式
Name: "{group}\{#MyAppName}"; Filename: "{app}\Client\{#ClientExeName}";
; 开始菜单中创建快捷方式：卸载应用的快捷方式
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
;WorkingDir: "{app}" 创建桌面快捷方式
Name: "{commondesktop}\SunRiseDemo"; Filename: "{app}\Client\{#ClientExeName}"; WorkingDir: "{app}\Client"; Check: IsFileExist('{#MyAppSetupBaseDir}{#MyAppName}\Client\{#ClientExeName}')

[Run]
;静默安装：Parameters: /q  
;安装C++运行时
Filename: "{app}\Common\vcredist2015-2019_x64.exe";Parameters: /q; StatusMsg: "Install C++ 2019 x86...";
;如果安装了网关，并且数据库没安装过，则安装数据库
Filename: "{app}\{#pgExe}"; StatusMsg: "Install postgresql-14.6-1-windows-x64.exe ..."; Check: IsPGNeedInstall; Components: Gateway; 

[UninstallDelete]     
Type: filesandordirs; Name: "{app}"
Type: filesandordirs; Name: "C:\ProgramData\Log"

[UninstallRun]    
Filename: "{app}\{#MyAppUnregisterGatewayBat}" ;  Flags: runhidden skipifdoesntexist